﻿/*  
	------------------------------------------------------------------------
	 NQ Core Library
	
	 Homepage: http://www.awzhome.de/
	------------------------------------------------------------------------
	
	This Source Code Form is subject to the terms of the Mozilla Public
	License, v. 2.0. If a copy of the MPL was not distributed with this
	file, You can obtain one at http://mozilla.org/MPL/2.0/.
   
	The Original Code is code of NQ Core Library.

	The Initial Developer of the Original Code is Andreas Weizel.
	Portions created by the Initial Developer are
	Copyright (C) 2012 Andreas Weizel. All Rights Reserved.
	
	Contributor(s): (none)
	
	------------------------------------------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AWZhome.NQ.Core.Fluent
{
	/// <summary>
	/// Wrapper class for logging functionality for usage with the fluent interface
	/// to the NQ Service Manager.
	/// </summary>
	/// <typeparam name="T">Type of the encapsulated service instance.</typeparam>
	public class LogWrapper<T>
	{
		private ServiceInstanceWrapper<T> _instanceWrapper;
		private T _serviceObject = default(T);
		private LogLevelWrapper _fileLevels;
		private LogLevelWrapper _eventLevels;
		private Exception _exception = null;

		internal LogWrapper(ServiceInstanceWrapper<T> instanceWrapper)
		{
			_instanceWrapper = instanceWrapper;
		}

		/// <summary>
		/// Sets a service object, whose information has to be included in the log.
		/// </summary>
		/// <param name="serviceObject">The service object.</param>
		/// <returns>Instance of current log wrapper.</returns>
		public LogWrapper<T> From(T serviceObject)
		{
			_serviceObject = serviceObject;
			return this;
		}

		/// <summary>
		/// Sets an exception object which will be included in log message.
		/// </summary>
		/// <param name="serviceObject">The service object.</param>
		/// <returns>Instance of current log wrapper.</returns>
		public LogWrapper<T> OnException(Exception exception)
		{
			_exception = exception;
			return this;
		}

		/// <summary>
		/// Creates an informational log message.
		/// </summary>
		/// <param name="logMessage">Message text. May include format definitions like in <see cref="System.String.Format"/></param>
		/// <param name="args">
		/// Additional arguments to be output in log,
		/// according to format definition in <paramref name="logMessage"/>.
		/// </param>
		/// <returns>The wrapper instance.</returns>
		public void Info(string logMessage, params object[] args)
		{
			NQServiceManager.Instance.LogMessage(_serviceObject, NQLogType.Info, _exception, logMessage, args);
		}

		/// <summary>
		/// Creates an informational log message from an exception.
		/// </summary>
		/// <param name="logException">Exception to log information from.</param>
		public void Info(Exception logException)
		{
			NQServiceManager.Instance.LogMessage(_serviceObject, NQLogType.Info, logException);
		}

		/// <summary>
		/// Creates a debug log message.
		/// </summary>
		/// <param name="logMessage">Message text. May include format definitions like in <see cref="System.String.Format"/></param>
		/// <param name="args">
		/// Additional arguments to be output in log,
		/// according to format definition in <paramref name="logMessage"/>.
		/// </param>
		public void Debug(string logMessage, params object[] args)
		{
			NQServiceManager.Instance.LogMessage(_serviceObject, NQLogType.Debug, _exception, logMessage, args);
		}

		/// <summary>
		/// Creates a debug log message from an exception.
		/// </summary>
		/// <param name="logException">Exception to log information from.</param>
		public void Debug(Exception logException)
		{
			NQServiceManager.Instance.LogMessage(_serviceObject, NQLogType.Debug, logException);
		}

		/// <summary>
		/// Creates an error log message.
		/// </summary>
		/// <param name="logMessage">Message text. May include format definitions like in <see cref="System.String.Format"/></param>
		/// <param name="args">
		/// Additional arguments to be output in log,
		/// according to format definition in <paramref name="logMessage"/>.
		/// </param>
		public void Error(string logMessage, params object[] args)
		{
			NQServiceManager.Instance.LogMessage(_serviceObject, NQLogType.Error, _exception, logMessage, args);
		}

		/// <summary>
		/// Creates an error log message from an exception.
		/// </summary>
		/// <param name="logException">Exception to log information from.</param>
		public void Error(Exception logException)
		{
			NQServiceManager.Instance.LogMessage(_serviceObject, NQLogType.Error, logException);
		}

		/// <summary>
		/// Creates a warning log message.
		/// </summary>
		/// <param name="logMessage">Message text. May include format definitions like in <see cref="System.String.Format"/></param>
		/// <param name="args">
		/// Additional arguments to be output in log,
		/// according to format definition in <paramref name="logMessage"/>.
		/// </param>
		public void Warning(string logMessage, params object[] args)
		{
			NQServiceManager.Instance.LogMessage(_serviceObject, NQLogType.Warning, _exception, logMessage, args);
		}

		/// <summary>
		/// Creates a warning log message from an exception.
		/// </summary>
		/// <param name="logException">Exception to log information from.</param>
		public void Warning(Exception logException)
		{
			NQServiceManager.Instance.LogMessage(_serviceObject, NQLogType.Warning, logException);
		}

		/// <summary>
		/// Gets/sets the active log levels for
		/// <see cref="AWZhome.NQ.Core.INQServiceManager.MessageLogged">MessageLogged</see> events.
		/// </summary>
		public LogLevelWrapper EventLevels
		{
			get
			{
				_eventLevels.LogType = NQServiceManager.Instance.LogEventFilter;
				return _eventLevels;
			}
			set
			{
				_eventLevels = value;
				NQServiceManager.Instance.LogEventFilter = _eventLevels;
			}
		}

		/// <summary>
		/// Gets/sets the active log levels for Service Manager's own log file.
		/// </summary>
		public LogLevelWrapper FileLevels
		{
			get
			{
				_fileLevels.LogType = NQServiceManager.Instance.LogFileFilter;
				return _fileLevels;
			}
			set
			{
				_fileLevels = value;
				NQServiceManager.Instance.LogFileFilter = _fileLevels;
			}
		}
	}
}
